<template>
  <div class="goods-image">
    <!-- 需要被监听坐标的目标元素 -->
    <div class="middle" ref="target">
      <!-- 主图 -->
      <img :src="images[activeIndex]" alt="" />
      <!-- 遮罩盒子 -->
      <div class="layer" :style="layerPos" v-show="!isOutside"></div>
    </div>
    <!-- 侧边小图 -->
    <ul class="small">
      <li
        v-for="(item, index) in images"
        :key="index"
        @mouseenter="activeIndex = index"
      >
        <img :src="item" alt="" />
      </li>
    </ul>

    <!-- 大图预览 -->
    <div class="large" v-show="!isOutside">
      <img :src="images[activeIndex]" :style="largePos" style="width:800px;height:800px" />
    </div>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    const activeIndex = ref(0)
    const target = ref(null)

    // 定义响应式数据，更新数据的时候更新视图
    const layerPos = reactive({ left: 0, top: 0 })
    const largePos = reactive({ left: 0, top: 0 })

    // isOutside            表示目标元素外面/里面
    // elementX/elementY    鼠标在盒子内的坐标
    const { elementX, elementY, isOutside } = useMouseInElement(target)

    // 监听鼠标的两个坐标值
    watch([elementX, elementY], () => {
      // -100  =>  让盒子和鼠标的中心对齐
      let left = elementX.value - 100
      let top = elementY.value - 100

      // 盒子的边界判断，左上右下的四个边界都要判断
      if (left < 0) left = 0
      if (top < 0) top = 0
      if (left > 200) left = 200
      if (top > 200) top = 200

      // 样式需要加 px 单位
      layerPos.left = left + 'px'
      layerPos.top = top + 'px'

      // 大图移动
      // -1   =>  反向移动
      // 2    =>  小图和大图的比例是 1:2 倍
      largePos.left = -1 * 2 * left + 'px'
      largePos.top = -1 * 2 * top + 'px'
    //   console.log(largePos)
    })

    return { activeIndex, isOutside, target, layerPos, largePos }
  }
}
</script>

<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
    overflow: hidden;
    img {
      max-width: 800px;
      max-height: 800px;
      position: absolute;
      top: 0;
      left: 0;
    }
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;

    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
